package com.zui.webapp.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.ss.formula.functions.T;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.zui.entity.BaseEntity;
import com.zui.manager.EntityManager;
import com.zui.manager.SessionManager;
import com.zui.security.Resource;
import com.zui.security.Role;
import com.zui.util.Page;
import com.zui.util.ZuiConstants;
/**
 * 角色
 * @author SHIJUN
 * @version 1.0 2008-9-18
 */
public class RoleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Role role;
	private List roleList = new ArrayList();
	private SessionManager sessionManager;
	
	private List resourceList = new ArrayList();
	private List<Resource> dispResources = new ArrayList();
	
	public String preAdd(){
		dispResources = sessionManager.findByNamedQuery("Resource.level2", null);
		return SUCCESS;
	}
	/**
	 * 保存角色
	 * @return
	 */
	public String saveRole(){
		if(role == null)
			return INPUT;
		String[] str = role.getResourcesInSelect().split(",");
		Set<Resource> rreSet = new LinkedHashSet<Resource>();
		for(int i=0 ;i<str.length; i++){
			rreSet.add(new Resource(Long.valueOf(str[i].trim())));
		}
		role.setResources(rreSet);
		if(role.getRoleId() != null)
			sessionManager.updateEntity(role);
		else{
			sessionManager.saveEntity(role);
		}
		return SUCCESS;
	}
	/**
	 * 修改角色
	 * @return
	 */
	public String updateRole(){
		if(role == null || role.getRoleId() == null)
			return INPUT;
		dispResources = (List<Resource>) sessionManager.findByNamedQuery("Resource.level2", null);
		role = (Role) super.getBaseManager().findById(role.getId());
		return SUCCESS;
	}
	/**
	 * 查询角色
	 * @return
	 */
	public String listRole(){
		DetachedCriteria criteria = getCriteria(role);
		roleList = super.getBaseManager().divPage(criteria, page==null?new Page():page);
		return SUCCESS;
	}
	
	public static DetachedCriteria getCriteria(Role role){
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		
		criteria.add(Restrictions.or(Restrictions.eq("isDeleted", BaseEntity.FLAG_N), Restrictions.isNull("isDelete")));
		if(role != null){
			if(role.getRoleName() != null && !role.getRoleName().trim().equals("")){
				criteria.add(Restrictions.like("roleName", role.getRoleName(),MatchMode.ANYWHERE));
			}
		}
		return criteria;
	}
	/**
	 * 删除
	 * @return
	 */
	public String deleteRole(){
		if(role == null || role.getId() == null)
			return INPUT;
		role = (Role) super.getBaseManager().findById(role.getId());
		role.setIsDeleted(ZuiConstants.IS_DELETED_Y);
		super.getBaseManager().merge(role);
		return SUCCESS;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}
	public List getResourceList() {
		return resourceManager.findByCriteria(ResourceAction.getCriteria());
	}
	public void setResourceList(List resourceList) {
		this.resourceList = resourceList;
	}
	public void setResourceManager(EntityManager<T, Serializable> resourceManager) {
		this.resourceManager = resourceManager;
	}
	public List<Resource> getDispResources() {
		return dispResources;
	}
	public void setDispResources(List<Resource> dispResources) {
		this.dispResources = dispResources;
	}
}
